package com.chinasoft.service.Impl;

import com.chinasoft.mapper.TeacherMapper;
import com.chinasoft.mapper.UserlistMapper;
import com.chinasoft.pojo.GoodCount;
import com.chinasoft.pojo.TeacherList;
import com.chinasoft.pojo.Userlist;
import com.chinasoft.service.UserlistService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class UserlistServiceImpl implements UserlistService {
    UserlistMapper userlistMapper;
    TeacherMapper teacherMapper;
    @Override
    public boolean addlist(Userlist userlist) {
        int num = userlistMapper.insert(userlist);
        if(num == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Userlist> getByName(String username) {//查询用户的课程记录
        List<Userlist> list = userlistMapper.getById(username);
        List<Userlist> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(30);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (Userlist user : list) {
            if ("0".equals(user.getDel()) && "团课".equals(user.getClasstype()) && (user.getTime().compareTo(dateTime) < 0)) {
                //当del=0且时间小于dateTime时
                String del = "1";
                userlistMapper.updateDel(user.getId(), del);
            }
            result.add(user);
        }
        return result;
    }

    @Override
    public boolean deleteById(Integer id) {
        Integer classid = userlistMapper.find1(id);
        TeacherList list = teacherMapper.getById(classid);
        Integer s = Integer.parseInt(list.getSoild()) - 1;
        String n = String.valueOf(s);
        int sum = teacherMapper.updateStatusByOrderCode(classid,n);
        int num = userlistMapper.delete(id);
        if(num == 1 && sum ==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Userlist> find(Userlist userlist) {
        return userlistMapper.findBy(userlist);
    }

    @Override
    public List<Userlist> search(String username, String del) {
        List<Userlist> list = userlistMapper.search(username, del);
        List<Userlist> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minusMinutes(30);//减去一个小时
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = oneHourAgo.format(formatter);
        for (Userlist user : list) {
            if ("0".equals(user.getDel()) && (user.getTime().compareTo(dateTime) < 0) ) {
                //当del=0且时间小于dateTime时
                String s = "1";
                userlistMapper.updateDel(user.getId(), s);
            }
            result.add(user);
        }
        return result;
    }

    @Override
    public List<Userlist> findById(Integer classid) {
        String del = "0";
        return userlistMapper.getId(classid,del);
    }

    @Override
    public List<GoodCount> getMonth() {
        return userlistMapper.getMonth();
    }

    @Override
    public boolean getClass(Integer classid, String username) {
        String del = "0";
        Integer num = userlistMapper.getclass(classid, username,del);
        if(num > 0){
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean getBytime(String time, String username) {
        String classtype = "团课";
        Integer num = userlistMapper.getBytime(time,classtype, username);
        if(num > 0){
            return true;
        } else {
            return false;
        }
    }
    @Override
    public boolean getTime(String teachername, String classtype, String time) {
        Integer num = userlistMapper.getTime(teachername,classtype,time);
        if(num == 1){
            return true;
        } else{
            return false;
        }
    }
}
